package com.itheima.web.listener;

import com.itheima.autoconfig.huawei.HuaWeiUGCTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component//动态监听器
@RocketMQMessageListener(consumerGroup = "movement-check-group", topic = "tanhua-movement-check")//绑定topic
//当topic中添加数据 运行程序
public class MovementCheckListener implements RocketMQListener<String> {
    @DubboReference
    private MovementService movementService;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String s) {
        //调用rpc根据接收到的动态id 查询动态详情
        Movement movement = movementService.findById(new ObjectId(s));
        //使用华为审核技术 审核动态发布的文字和图片
        boolean textCheck = huaWeiUGCTemplate.textContentCheck(movement.getTextContent());
        boolean imgCheck = huaWeiUGCTemplate.imageContentCheck(movement.getMedias().toArray(new String[movement.getMedias().size()]));
        //判断是否通过
        if (textCheck && imgCheck) {
            //审核通过把动态审核状态改为已审核
            movement.setState(1);
        } else {
            //不通过把动态审核状态改为已驳回
            movement.setState(2);
        }
        //调用rpc更新
        movementService.updateMovementById(movement);
        System.out.println("审核完成");
    }
}
